/**
 * 
 */
package GUI.PlayerPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import Actions.EndTurnAction;
import Actions.QuitAction;
import Player.Player;
import Utils.SpringUtilities;
import Utils.Utils;

//VS4E -- DO NOT REMOVE THIS LINE!
/**
 * @author Avia Tam
 * @author Assaf Miron
 *
 */
public class PlayerPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private JPanel pnlPicture;
    private JButton btnQuit;
    private JButton btnEnd;
    private JButton btnRolldice;
    private JPanel pnlButtons;
    private JPanel pnlPlayerDetails;
    private JLabel lblFunds;
    private JLabel lblGetOutOfJail;
    private String imgPath;
    private String imgLabel;
    private Player player;

    public PlayerPanel() {
        initComponents();
    }

    /**
     * Initialize the Panel Components
     */
    private void initComponents() {
        Dimension prefSize = new Dimension(180, 600);
        // Set the Layout
        setLayout(new GridBagLayout());
        // Make Transparent
        setOpaque(false);

        // Create Grid Bag Constrains (3 Rows, 1 Columns)
        GridBagConstraints top = new GridBagConstraints(0, 0, 1, 1, 0.5, 1, GridBagConstraints.PAGE_START, GridBagConstraints.BOTH, new Insets(0, 0, 20, 0), 0, 0);
        GridBagConstraints center = new GridBagConstraints(0, 1, 1, 1, 0.5, 2.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20, 0, 20, 0), 5, 20);
        GridBagConstraints bottom = new GridBagConstraints(0, 2, 1, 1, 0.5, 0, GridBagConstraints.PAGE_END, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);

        try {
            // Add the Buttons Panel
            add(getPnlButtons(), bottom);
            // Add the Details Panel
            add(getPlayerDetails(), center);
            // Add the Player Picture
            add(getPnlPicture(), top);
        } catch (Exception e) {
            Utils.writeText("Player Panel Init. Error!");
            e.printStackTrace();
        }
        // Set the Panel Preferred Size
        setPreferredSize(prefSize);
        // Set Borders to this Panel
        setBorder(BorderFactory.createLineBorder(Color.black));
        // Make Transparent
        setOpaque(false);
        // Repaint and Validate
        Utils.repaintAndValidateAll(this);
    }

    /**
     * Create the Players' Picture Panel
     * @return The Picture Panel
     */
    private JPanel getPnlPicture() {
        if (pnlPicture == null) {
            // Save the Image Path
            imgPath = "/defplayer.png";
            // Save the Image Label
            imgLabel = "Player Name";
            // Create a new Panel with Border LAyout
            pnlPicture = new JPanel(new BorderLayout());
            // Make transparent
            pnlPicture.setOpaque(true);
            // Get The Image
            ImageIcon image = Utils.getImageIcon(imgPath);
            // Check if the Image Exists
            if (image != null) {
                // Set the Preferred Size by the Image
                pnlPicture.setPreferredSize(new Dimension(100, 110));
                // Create an Image Label
                JLabel lblImage = new JLabel(image, JLabel.CENTER);
                // Set the Tool tip for the Image
                pnlPicture.setToolTipText(imgLabel);
                // Add the Image to the Panel
                pnlPicture.add(lblImage, BorderLayout.CENTER);
                // Add a Component Resize Listener
                pnlPicture.addComponentListener(new ResizeListener());
            }
        }
        return pnlPicture;
    }

    /**
     * Returns the Image Path
     * @return The Image Path
     */
    private String getImagePath() {
        return imgPath;
    }

    /**
     * Returns the Image Tool tip Text
     * @return The Tool tip Image Text
     */
    private String getImageLabel() {
        return imgLabel;
    }

    /**
     * Create the Player Details Panel
     * @return The Player Details Panel
     */
    private JPanel getPlayerDetails() {
        // Check if the Details Panel Exists
        if (pnlPlayerDetails == null) {
            // Create the Details Panel
            pnlPlayerDetails = new JPanel();
            // Set the Preferred Size
            Dimension prefSize = new Dimension(100, 300);
            // Clear the Panel
            pnlPlayerDetails.removeAll();
            // Create Labels to put in the Panel
            lblFunds = new JLabel("<HTML><U>Funds:</U></HTML>");
            lblGetOutOfJail = new JLabel("<HTML><U>Has Get Out of Prison Card?:</U></HTML>");
            // Add the Details Labels
            pnlPlayerDetails.add(lblFunds);
            pnlPlayerDetails.add(lblGetOutOfJail);
            // Set the Preferred Size
            pnlPlayerDetails.setPreferredSize(prefSize);
        }
        // Return the Panel
        return pnlPlayerDetails;
    }

    /**
     * Creates the Buttons Panel
     * @return The Buttons Panel
     */
    private JPanel getPnlButtons() {
        // Check if the Button Panel is Empty
        if (pnlButtons == null) {
            // Create the Panel
            pnlButtons = new JPanel(new SpringLayout());
            // Add the Buttons to the Panel
//			pnlButtons.add(getBtnEnd());
//			pnlButtons.add(getBtnRolldice());
            pnlButtons.add(getBtnQuit());

            // Organize the Buttons
            SpringUtilities.makeCompactGrid(pnlButtons, 1, pnlButtons.getComponentCount(), 0, 0, 5, 5);
        }
        // Return the Panel Component
        return pnlButtons;
    }

    /**
     * @return
     */
    private JButton getBtnRolldice() {
        // Check if the Button Exists
        if (btnRolldice == null) {
            // Create the Button
            btnRolldice = new JButton();
            btnRolldice.setText("Roll Dices");
        }
        // Return the Button
        return btnRolldice;
    }

    /**
     * Creates the Quit Button
     * @return The Quit Button
     */
    private JButton getBtnQuit() {
        // Check if the Button Exists
        if (btnQuit == null) {
            // Create the Button
            btnQuit = new JButton();
            btnQuit.setText("Quit");
        }
        // Return the Button
        return btnQuit;
    }

    /**
     * Creates the End Turn Button
     * @return The End Turn Button
     */
    private JButton getBtnEnd() {
        // Check if the Button Exists
        if (btnEnd == null) {
            // Create the Button
            btnEnd = new JButton();
            btnEnd.setText("End turn");

        }
        // Return the Button
        return btnEnd;
    }

    /**
     * @return The Current Playing Player
     */
    public Player getPlayer() {
        // Return the Current Playing Player
        return player;
    }

    /**
     * Sets the Players Details
     * @param player The Playing Player
     */
    public void setPlayerDetails(Player player) {
        // Save the Playing Player
        this.player = player;
        // Clear the Panel
        pnlPicture.removeAll();
        // Save the Player Name
        imgLabel = player.getName();
        // Set the Playre Name and Image
        setPlayerPictureAndName(player.getName());
        // Set the Buttons Action and Associate to Player
        setButtonsAction(player);
        // Set the Playing Player Color
        pnlPicture.setBackground(player.getColor());
        // Set the Player Details
        lblFunds.setText("<HTML><U>Funds:</U>" + player.getFunds().toString() + "</HTML>");
        lblGetOutOfJail.setText("<HTML><U>Has Get Out of Prison Card?:</U>" + player.getRelease_from_prison_card().toString() + "</HTML>");

        // Repaint and Validate
        Utils.repaintAndValidateAll(pnlPicture);
    }

    /**
     * Sets the Player Name And Picture to the Panel
     * @param name The Player Name
     */
    private void setPlayerPictureAndName(String name) {
        imgLabel = name;
        // Get The Image
        ImageIcon image = Utils.getImageIcon(imgPath);
        // Check if the Image Exists
        if (image != null) {
            // Set the Preferred Size by the Image
            pnlPicture.setPreferredSize(new Dimension(100, 110));
            // Create an Image Label
            JLabel lblImage = new JLabel(image, JLabel.CENTER);
            // Set the Tool tip for the Image
            pnlPicture.setToolTipText(imgLabel);
            // Add the Image to the Panel
            pnlPicture.add(lblImage, BorderLayout.CENTER);
        }
        // Add a Label
        pnlPicture.add(new JLabel(name, JLabel.CENTER), BorderLayout.SOUTH);
    }

    /**
     * Set the Buttons Action
     * @param player The Playing Player
     */
    private void setButtonsAction(Player player) {
        if (player.isHuman()) {
            // Enable the Buttons
//			btnEnd.setEnabled(true);
//			btnRolldice.setEnabled(true);
            // Set the Action for the End Turn and Quit Buttons
            btnQuit.setAction(new QuitAction(player)); // player quit game
//			btnEnd.setAction(new EndTurnAction());//player end turn
//			btnRolldice.setAction(new RollDiceAction());//player played
        } else {
            // Disable the Buttons
            btnQuit.setEnabled(false);
//			btnEnd.setEnabled(false);
//			btnRolldice.setEnabled(false);
        }
    }

    /**
     * Updates the Player Funds
     * @param funds The New Player Funds
     */
    public void updateFunds(Double funds) {
        lblFunds.setText("<HTML><U>Funds:</U>" + funds.toString() + "</HTML>");
    }

    /**
     * Updates if the Player has the Release from prison Card
     * @param release_from_prison_card Player has the Card?
     */
    public void updateGetOutOfPrison(Boolean release_from_prison_card) {
        lblGetOutOfJail.setText("<HTML><U>Has Get Out of Prison Card?:</U>" + release_from_prison_card.toString() + "</HTML>");

    }

    /**
     * Resize listener to ensure image is sized correctly for the panel
     *
     */
    class ResizeListener extends ComponentAdapter {

        /**
         * Overrides ComponentAdapter.componentResized
         * Ensures the image is correctly resized
         * @param evt the event
         */
        public void componentResized(ComponentEvent evt) {
            // Notify that the Component Resized
            super.componentResized(evt);
            // Check if this Component is Our Panel
            if (evt.getComponent() instanceof JPanel) {
                // Clear the Panel
                pnlPicture.removeAll();
                // Create a new Resized Image
                ImageIcon resizedIcon = Utils.createScaledIcon(getImagePath(), pnlPicture.getWidth(), pnlPicture.getHeight(), pnlPicture.getInsets());
                // Recreate the Picture Panel
                JLabel img = new JLabel(resizedIcon);
                // Set the Label Tool tip
                pnlPicture.setToolTipText(getImageLabel());
                // Add the Image
                pnlPicture.add(img, BorderLayout.CENTER);
                // Add the Image Label
                pnlPicture.add(new JLabel(getImageLabel(), JLabel.CENTER), BorderLayout.SOUTH);
                // Repaint and Validate
                Utils.repaintAndValidateAll(PlayerPanel.this);
            }
        }
    }
}
